In [33]:
import nengo
import numpy as np
class Steps(nengo.processes.Process):
def __init__(self, width):
self.width = width
def make_step(self, size_in, size_out, dt, rng):
last_index = np.array([-1])
last_value = np.zeros(size_out)
def step(t):
index = int(t / self.width)
if index != last_index:
last_value[:] = rng.uniform(-1, 1, size_out)
last_index[:] = index
return last_value
return step
class Delay(nengo.processes.Process):
def __init__(self, delay):
self.delay = delay
def make_step(self, size_in, size_out, dt, rng):
timesteps = int(self.delay / dt)
history = np.zeros((timesteps + 1, size_out))
def step(t, x):
history[:] = np.roll(history, -1)
history[-1] = x
return history[0]
return step
model = nengo.Network()
with model:
stim = nengo.Node(Steps(width=0.1), size_out=1)
delay = nengo.Node(Delay(delay=0.05), size_in=1, size_out=1)
nengo.Connection(stim, delay, synapse=None)
probe1 = nengo.Probe(stim)
probe2 = nengo.Probe(delay)
sim = nengo.Simulator(model, seed=1)
sim.run(1)
plot(sim.trange(), sim.data[probe1])
plot(sim.trange(), sim.data[probe2])
show()
In [34]:
# run the model again to make sure resetting works
sim = nengo.Simulator(model, seed=1)
sim.run(1)
plot(sim.trange(), sim.data[probe1])
plot(sim.trange(), sim.data[probe2])
show()
sim.reset()
sim.run(1)
plot(sim.trange(), sim.data[probe1])
plot(sim.trange(), sim.data[probe2])
show()
In [5]:
import nengo
import numpy as np
class Steps:
def __init__(self, width):
self.width = width
def build(self, node, dt, rng):
last_index = np.array([-1])
last_value = np.zeros(node.size_out)
def step(t):
index = int(t / self.width)
if index != last_index:
last_value[:] = rng.uniform(-1, 1, node.size_out)
last_index[:] = index
return last_value
return step
class Delay:
def __init__(self, delay):
self.delay = delay
def build(self, node, dt, rng):
timesteps = int(self.delay / dt)
history = np.zeros((timesteps + 1, node.size_out))
def step(t, x):
history[:] = np.roll(history, -1)
history[-1] = x
return history[0]
return step
model = nengo.Network()
with model:
stim = nengo.Node(Steps(width=0.1), size_out=1)
delay = nengo.Node(Delay(delay=0.05), size_in=1, size_out=1)
nengo.Connection(stim, delay, synapse=None)
probe1 = nengo.Probe(stim)
probe2 = nengo.Probe(delay)
sim = nengo.Simulator(model, seed=1)
sim.run(1)
plot(sim.trange(), sim.data[probe1])
plot(sim.trange(), sim.data[probe2])
show()
__call__
method, since then it acts like a standard Python callableself.
approach.self
that is used when running will never be the same instance as the one the user madeprint self
for debugging purposes, they may get confusedself.original
to point to the original object
In [3]:
import nengo
import numpy as np
class Steps:
def __init__(self, width):
self.width = width
def build(self, node, dt, rng):
self.last_index = None
self.last_value = np.zeros(node.size_out)
self.rng = rng
def __call__(self, t):
index = int(t / self.width)
if index != self.last_index:
self.last_value = self.rng.uniform(-1, 1, self.last_value.shape)
self.last_index = index
return self.last_value
class Delay:
def __init__(self, delay):
self.delay = delay
def build(self, node, dt, rng):
timesteps = int(self.delay / dt)
self.history = np.zeros((timesteps + 1, node.size_out))
def __call__(self, t, x):
self.history[:] = np.roll(self.history, -1)
self.history[-1] = x
return self.history[0]
model = nengo.Network()
with model:
stim = nengo.Node(Steps(width=0.1), size_out=1)
delay = nengo.Node(Delay(delay=0.05), size_in=1, size_out=1)
nengo.Connection(stim, delay, synapse=None)
probe1 = nengo.Probe(stim)
probe2 = nengo.Probe(delay)
sim = nengo.Simulator(model, seed=1)
sim.run(1)
plot(sim.trange(), sim.data[probe1])
plot(sim.trange(), sim.data[probe2])
show()
In [4]:
# run the model again to make sure resetting works
sim = nengo.Simulator(model, seed=1)
sim.run(1)
plot(sim.trange(), sim.data[probe1])
plot(sim.trange(), sim.data[probe2])
show()
sim.reset()
sim.run(1)
plot(sim.trange(), sim.data[probe1])
plot(sim.trange(), sim.data[probe2])
show()
In [ ]: